import { saveAs } from 'file-saver';
import { IMenu } from '@/models/menu.interface';

export const getDomain = () => {
  const { protocol, host } = window.location;
  const href = `${protocol}//${host}`;

  const lastChar = href[href.length - 1];
  let result = href;
  if (lastChar === '/') {
    result = href.substring(0, href.length - 1);
  }
  return result;
};

export const listToTree = (menus: IMenu[] = [], pid = 0) => {
  const result: IMenu[] = [];
  menus.forEach(menu => {
    if (menu.pid === pid) {
      const newMenu = { ...menu, children: listToTree(menus, menu.id) };
      result.push(newMenu);
    }
  });
  return result;
};

export const findParents = (menus: IMenu[] = [], pid = 0): IMenu[] => {
  const result: IMenu[] = [];
  menus?.forEach(menu => {
    if (menu.id === pid) {
      result.push(...findParents(menus, menu.pid), menu);
    }
  });
  return result;
};

export const unescapeHtml = (html: string) => {
  return html
    .replace(html ? /&(?!#?\w+;)/g : /&/g, '&amp;')
    .replace(/&lt;/g, '<')
    .replace(/&gt;/g, '>')
    .replace(/&quot;/g, '"')
    .replace(/&#39;/g, "'");
};

// export const getQueryString = name => {
//   const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
//   const r = window.location.search.substr(1).match(reg);
//   if (r != null) return unescape(decodeURI(r[2]));
//   return null;
// };

// export const serialize = data => {
//   const list = [];
//   Object.keys(data).forEach(ele => {
//     list.push(`${ele}=${data[ele]}`);
//   });
//   return list.join('&');
// };

export const encodeserialize = (data: Record<string, string | number | boolean>) => {
  const list: string[] = [];
  Object.keys(data).forEach(ele => {
    list.push(`${ele}=${encodeURIComponent(data[ele])}`);
  });
  return list.join('&');
};

// export const deepCopy = (obj, cache = []) => {
//   // just return if obj is immutable value
//   if (obj === null || typeof obj !== 'object') {
//     return obj;
//   }

//   // if obj is hit, it is in circular structure
//   const hit = find(cache, c => c.original === obj);
//   if (hit) {
//     return hit.copy;
//   }

//   const copy = Array.isArray(obj) ? [] : {};
//   // put the copy into cache at first
//   // because we want to refer it in recursive deepCopy
//   cache.push({
//     original: obj,
//     copy,
//   });

//   Object.keys(obj).forEach(key => {
//     copy[key] = deepCopy(obj[key], cache);
//   });

//   return copy;
// };
// // 千分符
// export const toThousands = num => {
//   return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
// };
// // 判断字符串是否是数字
// export const isNumber = val => {
//   const regPos = /^\d+(\.\d+)?$/; // 非负浮点数
//   const regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; // 负浮点数
//   if (regPos.test(val) || regNeg.test(val)) {
//     return true;
//   } else {
//     return false;
//   }
// };

export const download = (row: any) => {
  fetch(row.link)
    .then(res => res.blob())
    .then(blob => {
      saveAs(blob, row.originalName);
    });
};

// export const htmlEscape = text => {
//   return text.replace(/[<>"&]/g, function(match) {
//     switch (match) {
//       case '<':
//         return '&lt;';
//       case '>':
//         return '&gt;';
//       case '&':
//         return '&amp;';
//       case '"':
//         return '&quot;';
//     }
//   });
// };
